package com.dc.toolkit.hutool.core;

import cn.hutool.core.date.LocalDateTimeUtil;
import org.junit.Test;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Description: Hutool LocalDateTimeUtil Demo
 * @author: gdc
 * @date: 2021/5/8
 * @version 1.0
 */
public class LocalDateTimeUtilTest {

    private LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2021, 5, 8, 21, 30, 0);
    private LocalDate LOCAL_DATE = LocalDate.of(2021, 5, 8);
    private LocalDateTime NOW = LocalDateTime.now();

    /**
     * 测试   Date 转 LocalDateTime,时区为当前时区
     */
    @Test
    public void testDate2LocalDateTime(){
        Date date = new Date();
        LocalDateTime ldt = LocalDateTimeUtil.of(date);

        System.out.println(ldt);
        System.out.println(date);
    }

    /**
     * 测试   日期字符串转 LocalDateTime,仅支持yyyy-MM-dd'T'HH:mm:ss格式，例如：2007-12-03T10:15:30
     */
    @Test
    public void testStrParseLocalDateTime(){
        LocalDateTime ldt = LocalDateTimeUtil.parse("2021-05-08T21:30:00");
        System.out.println(ldt);
    }

    /**
     * 测试   日期字符串转 LocalDateTime(给定日期格式)
     */
    @Test
    public void testStrParseLocalDateTime2(){
        LocalDateTime ldt = LocalDateTimeUtil.parse("2021-05-08 21:30:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        assertThat(LOCAL_DATE_TIME, equalTo(ldt));

        LocalDateTime ldt2 = LocalDateTimeUtil.parse("2021-05-08", DateTimeFormatter.ISO_LOCAL_DATE);
        assertThat(LocalDateTime.of(2021, 5, 8, 0, 0), equalTo(ldt2));

        LocalDateTime ldt3 = LocalDateTimeUtil.parse("2021-05-08 21:30:00", "yyyy-MM-dd HH:mm:ss");
        assertThat(LOCAL_DATE_TIME, equalTo(ldt3));
    }

    /**
     * 测试   日期字符串转 LocalDate
     */
    @Test
    public void testStrParseLocalDate(){
        LocalDate ld = LocalDateTimeUtil.parseDate("2021-05-08");
        assertThat(LOCAL_DATE, equalTo(ld));

        LocalDate ld2 = LocalDateTimeUtil.parseDate("2021/05/08", "yyyy/MM/dd");
        assertThat(LOCAL_DATE, equalTo(ld2));

        LocalDate ld3 = LocalDateTimeUtil.parseDate("20210508", DateTimeFormatter.ofPattern("yyyyMMdd"));
        assertThat(LOCAL_DATE, equalTo(ld3));
    }

    /**
     * 测试   格式化日期格式 为 yyyy-MM-dd HH:mm:ss 和 yyyy-MM-dd
     */
    @Test
    public void testFormatNormal(){
        System.out.println(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        System.out.println(LocalDateTimeUtil.formatNormal(LocalDate.now()));
    }

    /**
     * 测试   格式化日期格式 为 yyyy-MM-dd HH:mm:ss 和 yyyy-MM-dd
     */
    @Test
    public void testFormat(){
        System.out.println(LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy/MM/dd HH:mm:ss"));
        System.out.println(LocalDateTimeUtil.format(LocalDate.now(), "yyyy/MM/dd"));

        System.out.println(LocalDateTimeUtil.format(LocalDateTime.now(), DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")));
        System.out.println(LocalDateTimeUtil.format(LocalDate.now(), DateTimeFormatter.ofPattern("yyyy/MM/dd")));
    }

    /**
     * 测试 获取两个日期的差，如果结束时间早于开始时间，获取结果为负。
     */
    @Test
    public void testBetween(){
        Duration duration = LocalDateTimeUtil.between(LOCAL_DATE_TIME, NOW);
        System.out.println(duration);

        long seconds = LocalDateTimeUtil.between(LOCAL_DATE_TIME, NOW, ChronoUnit.SECONDS);
        System.out.println(seconds);
    }

    /**
     * 测试   修改为一天的开始时间，例如：2020-02-02 00:00:00,000
     */
    @Test
    public void testBeginOfDay(){
        System.out.println(NOW);
        System.out.println(LocalDateTimeUtil.beginOfDay(NOW));
    }

    /**
     * 测试   修改为一天的结束时间，例如：2020-02-02 23:59:59,999
     */
    @Test
    public void testEndOfDay(){
        System.out.println(NOW);
        System.out.println(LocalDateTimeUtil.endOfDay(NOW));
    }
}
